/* eslint-disable */
const { Service } = require('egg');

class FeedbackService extends Service {
    async findList(query) {
        const { ctx } = this;
        const { user_id, type, status, pageIndex = 1, pageSize = 10 } = query;
        const searchConditions = {};
        if (user_id) {
            searchConditions.user_id = user_id;
        }
        if (type) {
            // 精确查找反馈类型
            searchConditions.type = type;
        }
        if (status) {
            // 精确查找反馈状态
            searchConditions.status = status;
        }

        const offset = (pageIndex - 1) * pageSize;
        return await ctx.model.Feedback.findAndCountAll({
            where: searchConditions,
            include: [
                {
                    model: ctx.model.User,
                    as: 'user'
                }
            ],
            offset: Number(offset),
            limit: Number(pageSize)
        });
    }
    async add(feedback) {
        const { ctx } = this;
        return await ctx.model.Feedback.create(feedback);
    }

    async remove(id) {
        const { ctx } = this;
        const affectedRows = await ctx.model.Feedback.destroy({
            where: { id },
        });
        return !!affectedRows;
    }

    async modify(status, id) {
        const { ctx } = this;
        const [affectedRows] = await ctx.model.Feedback.update(status, {
            where: { id },
        });
        return !!affectedRows;
    }
    async findById(id) {
        const { ctx } = this;
        return await ctx.model.Feedback.findByPk(id);
    }
}

module.exports = FeedbackService;
